package com.lishe.lishe.lishe.module.excitation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.excitation.activity.DianZanDetailsActivity;
import com.lishe.lishe.lishe.module.excitation.activity.PersonExcitationActivity;
import com.lishe.lishe.lishe.module.excitation.bean.CommentObject;
import com.lishe.lishe.lishe.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by shino on 2017/10/22.
 */

public class CommentAdapter extends RecyclerviewBaseAdapter {
    private Context context;
    private LinkedList<CommentObject> commentObjects;
    private ImageLoaderUtils imageUtils;

    public CommentAdapter(Context context, LinkedList<CommentObject> commentObjects) {
        this.context = context;
        this.commentObjects = commentObjects;
        imageUtils = new ImageLoaderUtils();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_activity_dianzan_comment, null);
        return new CommentHolder(view);
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, int position) {
        final CommentObject comment = commentObjects.get(position);
        if (!TextUtils.isEmpty(comment.getAvatarPath()))
            imageUtils.displayImage(comment.getAvatarPath(), ((CommentHolder) holder).head);
        ((CommentHolder) holder).content.setText(StringUtils.utf8ToString(comment.getContent()));
        ((CommentHolder) holder).name.setText(comment.getUserName());
        ((CommentHolder) holder).time.setText(comment.getCreateTime());

        ((CommentHolder) holder).head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(comment.getUserType()) && !comment.getUserType().equals("0")) {//公司
                    //点击头像跳转个人中心
                    PersonExcitationActivity.jump((DianZanDetailsActivity) context, String.valueOf(comment.getUserId()));
                }
                if (TextUtils.isEmpty(comment.getUserType())) {
                    PersonExcitationActivity.jump((DianZanDetailsActivity) context, String.valueOf(comment.getUserId()));
                }
            }
        });
        ((CommentHolder) holder).name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(comment.getUserType()) && !comment.getUserType().equals("0")) {//公司
                    //点击头像跳转个人中心
                    PersonExcitationActivity.jump((DianZanDetailsActivity) context, String.valueOf(comment.getUserId()));
                }
                if (TextUtils.isEmpty(comment.getUserType())) {
                    PersonExcitationActivity.jump((DianZanDetailsActivity) context, String.valueOf(comment.getUserId()));
                }
            }
        });
    }

    @Override
    public int getItemCountMy() {
        return commentObjects.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    class CommentHolder extends RecyclerView.ViewHolder {
        private ImageView head;
        private TextView name;
        private TextView time;
        private TextView content;

        public CommentHolder(View v) {
            super(v);
            head = v.findViewById(R.id.head);
            name = v.findViewById(R.id.name);
            time = v.findViewById(R.id.time);
            content = v.findViewById(R.id.content);
        }
    }
}
